package engine.fov;

import java.util.HashSet;
import java.util.Set;

/**
 * A LOS board that records points that were visited, while using another board to decide obstacles.
 * 
 * @author sdatta
 */
class RecordVisitBoard implements Board, GenericCalculateProjection.VisitedBoard {

	Board b;

	boolean calculateProject;

	boolean endVisited = false;

	int sx, sy, sxy;

	int targetX, targetY;

	private final Point2I visitedCheck = new Point2I(0, 0);

	// int manhattanDist;
	Set<Point2I> visitedNotObs = new HashSet<Point2I>();

	public RecordVisitBoard(Board b, int sx, int sy, int dx, int dy, boolean calculateProject) {
		super();
		this.b = b;
		this.sx = sx;
		this.sy = sy;
		sxy = sx + sy;
		targetX = dx;
		targetY = dy;

		this.calculateProject = calculateProject;
	}

	public boolean contains(int x, int y) {
		return b.contains(x, y);
	}

	@Override
	public boolean isLit(int x, int y) {
		return b.isLit(x, y);
	}

	public boolean isObstruction(int x, int y) {
		return b.isObstruction(x, y);
	}

	public void visit(int x, int y) {
		// System.out.println("visited "+x+" "+y);
		if (x == targetX && y == targetY) {
			endVisited = true;
		}
		if (calculateProject && !b.isObstruction(x, y)) {
			int dx = x - sx;
			dx = dx > 0 ? dx : -dx;
			int dy = y - sy;
			dy = dy > 0 ? dy : -dy;
			visitedNotObs.add(new Point2I(dx, dy));
		}
		// DEBUG
		// b.visit(x, y);
	}

	public boolean wasVisited(int x, int y) {
		visitedCheck.x = x;
		visitedCheck.y = y;
		return visitedNotObs.contains(visitedCheck);
	}

}